<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use App\Lib\JwtAuth;
use App\Http\Controllers\Api\BaseController;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class ApiAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
//        dump($request);
        $token = $request->header('token','');
        if(!$token){
           $baseController = new BaseController();
           return $baseController->fail(503002);
        }
        if(!JwtAuth::verifyToken($token)){
            $baseController = new BaseController();
            return $baseController->fail(503003);
        }
        $uid = JwtAuth::getUserId($token,'uid');
        $request->merge(['uid' => $uid]);
        return $next($request);
    }
}
